<?php

namespace addons\wwh\taglib;

use think\Db;
use think\template\TagLib;
use fast\Random;


class Wwh extends TagLib
{

    /**
     * 定义标签列表
     */
    protected $tags = [
        // 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
        'market'   => ['attr' => 'id,empty,key,mod', 'close' => 1],
        'paginate'   => ['attr' => 'id,table,field,order,page', 'close' => 1],
        'newstop'   => ['attr' => 'id,empty,key,mod,limit', 'close' => 1],
        'development'   => ['attr' => 'id,empty,key,mod', 'close' => 1],
        'cvsearch'   => ['attr' => 'id,table,field', 'close' => 1],
        'join'   => ['attr' => 'id,name,page', 'close' => 1],
    ];

    public function tagMarket($tag, $content){
        $id = $tag['id'];
        $empty = isset($tag['empty']) ? $tag['empty'] : '';
        $key = !empty($tag['key']) ? $tag['key'] : 'i';
        $mod = isset($tag['mod']) ? $tag['mod'] : '2';

        $var = Random::alnum(10);
        $parse = '<?php ';
        $parse .= '$__' . $var . '__ = \addons\wwh\model\Archives::getMarket();';
        $parse .= ' ?>';
        $parse .= '{volist name="$__' . $var . '__" id="' . $id . '" empty="' . $empty . '" key="' . $key . '" mod="' . $mod . '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }

    public function tagPaginate($tag, $content){
        $table = $tag['table'];
        $field = $tag['field'];
        $order = $tag['order'];
        $page = $tag['page'];

        $php = <<<php
<?php
       \$list = think\Db::name('$table')->field('$field')->order('$order')->paginate('$page', false, ['query' => request()->param()]);
       \$__LIST__ = \$list;
?>
php;

        $id = $tag['id'];
        $parse = $php;
        $parse .= '{volist name="__LIST__" id="' . $id . '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }

    public function tagNewsTop($tag, $content){
        $id = $tag['id'];
        $empty = isset($tag['empty']) ? $tag['empty'] : '';
        $key = !empty($tag['key']) ? $tag['key'] : 'i';
        $mod = isset($tag['mod']) ? $tag['mod'] : '2';
        $limit = $tag['limit'];

        $var = Random::alnum(10);
        $parse = '<?php ';
        $parse .= '$__' . $var . '__ = \addons\wwh\model\Archives::getNewsTop('.$limit.');';
        $parse .= ' ?>';
        $parse .= '{volist name="$__' . $var . '__" id="' . $id . '" empty="' . $empty . '" key="' . $key . '" mod="' . $mod . '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }

    public function tagDevelopment($tag, $content){
        $id = $tag['id'];
        $empty = isset($tag['empty']) ? $tag['empty'] : '';
        $key = !empty($tag['key']) ? $tag['key'] : 'i';
        $mod = isset($tag['mod']) ? $tag['mod'] : '2';

        $var = Random::alnum(10);
        $parse = '<?php ';
        $parse .= '$__' . $var . '__ = \addons\wwh\model\Archives::getDevelopment();';
        $parse .= ' ?>';
        $parse .= '{volist name="$__' . $var . '__" id="' . $id . '" empty="' . $empty . '" key="' . $key . '" mod="' . $mod . '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }

    public function tagCvSearch($tag, $content){
        $table = $tag['table'];
        $field = $tag['field'];

        $php = <<<php
<?php
       \$list = think\Db::name('$table')->distinct(true)->field('$field')->select();
       \$__LIST__ = \$list;
?>
php;

        $id = $tag['id'];
        $parse = $php;
        $parse .= '{volist name="__LIST__" id="' . $id . '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }

    public function tagJoin($tag, $content) {
        $page = $tag['page'];
        $php = <<<php
<?php
        \$keywords = input('keywords');
        \$branch = input('branch');
        \$address = input('address');
        if (empty(\$keywords || \$branch || \$address)) {
            \$data5['list'] = think\Db::name('wwh_position')->order('time desc')->paginate('$page', false, ['query' => request()->param()]);
        } else {
            \$map['name'] = ['like','%'.\$keywords.'%'];
            \$map['dept'] = ['like','%'.\$branch.'%'];
            \$map['addr'] = ['like','%'.\$address.'%'];
            \$data5['list'] = think\Db::name('wwh_position')->where(\$map)->order('time desc')->paginate('$page', false, ['query' => request()->param()]);
        }
        \$__LIST__ = \$data5['list'];
?>
php;
        $id = $tag['id'];
        $parse = $php;
        $parse .= '{volist name="__LIST__" id="' . $id . '"}';
        $parse .= $content;
        $parse .= '{/volist}';
        return $parse;
    }

}
